import { _decorator, animation, AnimationClip, Animation, Component, Node, Sprite, UITransform, Vec3 } from "cc"
import { TILE_HEIGHT, TILE_WITTH } from "../Tile/TileManager"
import ResourceManager from "../../Runtime/ResourceManager"
import {
  CONTROLLER_ENUM,
  DIRECTION_ENUM,
  DRECTION_ORDER_ENUM,
  ENTITY_STATE_ENUM,
  EVENT_ENUM,
  PARAME_NAME_ENUM
} from "../../Enums"
import EventManager from "../../Runtime/EventManager"
import { PlayerStateMachine } from "./PlayerStateMachine"
import { ANIMATION_SPEED } from "../../base/state"
const { ccclass, property } = _decorator

@ccclass("PlayerManager")
export class PlayerManager extends Component {
  x = 0
  y = 0
  targetY = 0
  targetX = 0
  readonly speed = 0.1
  fsm
  private _direction: DIRECTION_ENUM
  private _state: ENTITY_STATE_ENUM

  get direction() {
    return this._direction
  }
  get state() {
    return this._state
  }
  set direction(newDirection: DIRECTION_ENUM) {
    this._direction = newDirection
    this.fsm.setParams(PARAME_NAME_ENUM.DIRECTION, DRECTION_ORDER_ENUM[newDirection])
  }
  set state(newState) {
    this._state = newState
    this.fsm.setParams(newState, true)
  }
  start() {
    EventManager.on(EVENT_ENUM.PLAYER_CTRL, this.move, this)
  }
  update(deltaTime: number) {
    this.updateXY()
    this.node.setPosition(this.x * TILE_WITTH - 1.5 * TILE_WITTH, this.y * TILE_HEIGHT + 1.5 * TILE_WITTH)
  }
  updateXY() {
    const cx = this.targetX - this.x
    const cy = this.targetY - this.y

    if (cx < 0) {
      this.x -= this.speed
    } else if (cx > 0) this.x += this.speed
    if (cy < 0) {
      this.y -= this.speed
    } else if (cy > 0) this.y += this.speed
    if (Math.abs(this.x - this.targetX) < 0.1 && Math.abs(this.y - this.targetY) < 0.1) {
      this.x = this.targetX
      this.y = this.targetY
    }
  }

  move(inputDir) {
    if (inputDir === CONTROLLER_ENUM.TOP) {
      this.targetY += 1
    } else if (inputDir === CONTROLLER_ENUM.BOTTOM) {
      this.targetY -= 1
    } else if (inputDir === CONTROLLER_ENUM.LEFT) {
      this.targetX -= 1
    } else if (inputDir === CONTROLLER_ENUM.RIGHT) {
      this.targetX += 1
    } else if (inputDir === CONTROLLER_ENUM.TURNLEFT) {
      if (this.direction == DIRECTION_ENUM.TOP) {
        this.direction = DIRECTION_ENUM.LEFT
      }
      if (this.direction == DIRECTION_ENUM.LEFT) {
        this.direction = DIRECTION_ENUM.BOTTOM
      }
      if (this.direction == DIRECTION_ENUM.BOTTOM) {
        this.direction = DIRECTION_ENUM.RIGHT
      }
      if (this.direction == DIRECTION_ENUM.RIGHT) {
        this.direction = DIRECTION_ENUM.TOP
      }
    }
  }
  async initPlayer() {
    const sprite = this.addComponent(Sprite)
    sprite.sizeMode = Sprite.SizeMode.CUSTOM
    const transform = this.getComponent(UITransform)
    transform.setContentSize(TILE_WITTH * 4, TILE_HEIGHT * 4)
    // const imgs = await ResourceManager.loadRes("texture/player/idle/top/idle")
    this.fsm = this.addComponent(PlayerStateMachine)
    await this.fsm.init()
    this.direction = DIRECTION_ENUM.TOP
    this.state = ENTITY_STATE_ENUM.TURENLEFT
    // this.fsm.setParams(PARAME_NAME_ENUM.IDLE, true)
  }
}
